package com.mensanyun.yminder.core.connector

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Stroke
import com.mensanyun.yminder.core.data.Topic
import com.mensanyun.yminder.core.layout.ContentLayoutConstrains

interface Connector {
    fun connect(rootTopic: Topic, drawScope: DrawScope)
}


object DefaultConnector : Connector {
    override fun connect(rootTopic: Topic, drawScope: DrawScope) {
        drawUnderline(drawScope, rootTopic)
        for (topic in rootTopic.subtopics()) {
            val path = Path()
            val startx = rootTopic.layoutProperties.x + rootTopic.layoutProperties.width
            val starty = rootTopic.layoutProperties.y + rootTopic.layoutProperties.height

            val endx = topic.second.layoutProperties.x
            val endy = topic.second.layoutProperties.y + topic.second.layoutProperties.height

            val controlx = (startx + endx) / 2
            val control1y = starty
            val control2y = endy
            path.moveTo(
                startx, starty
            )
            path.cubicTo(
                controlx, control1y,
                controlx, control2y,
                endx, endy
            )
            drawScope.drawPath(path, Color.Black, style = Stroke(width = 1f))
        }
    }

    private fun drawUnderline(
        drawScope: DrawScope,
        rootTopic: Topic
    ) {
        drawScope.drawLine(
            Color.Black,
            Offset(
                rootTopic.layoutProperties.x,
                rootTopic.layoutProperties.y + rootTopic.layoutProperties.height
            ),
            Offset(
                rootTopic.layoutProperties.x + rootTopic.layoutProperties.width,
                rootTopic.layoutProperties.y + rootTopic.layoutProperties.height
            )
        )
        for (topic in rootTopic.subtopics()) {
            connect(topic.second, drawScope)
        }
    }

}